#!/usr/bin/env node
"use strict";

require("./util/prototype.js");
const fileUtil = require("./util/fileUtil");
const xlsx = require("node-xlsx");

module.exports = function(workFolder) {
    fileUtil
        .readDirPromise(
            workFolder,
            file => file.endsWith(".xls") && file.startsWith("_")
        )
        .then(files => {
            files.map(file => {
                var excelFilePath = `${workFolder}${file}`;

                xlsx.parse(excelFilePath)
                    .filter(
                        sheet => sheet.name.toLowerCase().indexOf("code") != -1
                    )
                    .map(sheet => {
                        sheet.name = sheet.name
                            .toLowerCase()
                            .replace("code", "");
                        let ejsFilePath = `./../ejs/code.ejs`;
                        fileUtil
                            .renderFilePromise(ejsFilePath, sheet)
                            .then(str => {
                                console.log(str);
                            })
                            .catch(err => {
                                console.log(err);
                            });
                    });
            });
        });
};
